package light.head.dao.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.OrganTypeHandle;
import light.head.model.OrganTypeHandlePK;

import org.springframework.stereotype.Service;

@Service
public class OrganTypeHandleDAO extends BaseDAO<OrganTypeHandle, OrganTypeHandlePK> {

	private final static String sql_findByOrganTypeCode = "FROM OrganTypeHandle oth WHERE oth.otc = ?";
	private final static String sql_deleteByOrganTypeCode = "DELETE OrganTypeHandle ath WHERE ath.otc = ?";
	
	/**
	 * 根据帐号类型编号获取该帐号类型可以做的操作
	 * @param	帐号类型编号
	 */
	public Map<String, Boolean> getHandleMapByOrganTypeCode(Integer code) {
		if (null == code)
			return null;
		
		Map<String, Boolean> handleMap = null;
		handleMap = Cache.organTypeMap.get(code);
		if (null == handleMap)
			handleMap = loadHandleMapByOrganTypeCode(code);
		
		return handleMap;
	}
	
	/**
	 * 加载指定机构类型对应的所有操作
	 * @param	机构类型编号
	 */
	public Map<String, Boolean> loadHandleMapByOrganTypeCode(Integer code) {
		List<OrganTypeHandle> othList = find(sql_findByOrganTypeCode, code);
		if (null == othList || othList.size() == 0)
			return null;
		
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		for (OrganTypeHandle oth : othList)
			result.put(oth.getHc(), true);
		
		Cache.organTypeMap.put(code, result);
		return result;
	}

	/**
	 * 根据帐号类型编号获取该类型对应的所有OrganTypeHandle对象
	 * @param	帐号类型编号
	 */
	public List<OrganTypeHandle> findByOrganTypeCode(Integer organTypeCode) {
		List<OrganTypeHandle> list = find(sql_findByOrganTypeCode, organTypeCode);
		return list;
	}
	
	/**
	 * 删除与指定帐号类型相关的所有帐号类型操作对象
	 * @param	权限编号
	 */
	public RESULT deleteByOrganTypeCode(Integer organTypeCode) {
		execute(sql_deleteByOrganTypeCode, organTypeCode);
		return RESULT.SUCCESS;
	}
}
